package my.assistant.reminder;

import my.assistant.R;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Handler;
import android.util.Log;

/**
 * This receiver will be created when it receive message every time.
 */
public class AlarmReceiver extends BroadcastReceiver
{

	private static MediaPlayer mediaPlayer;

	private Handler handler = new Handler();

	@Override
	public void onReceive(Context context, Intent intent)
	{
		Log.e("rec", "---------alarm-------");
		if (mediaPlayer == null)
		{
			mediaPlayer = new MediaPlayer();
		}
		AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.s_dream);
		if (mediaPlayer.isPlaying())
		{
			mediaPlayer.stop();
			mediaPlayer.reset();
		}
		try
		{
			mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
			afd.close();
			mediaPlayer.prepare();
			mediaPlayer.start();
		} catch (Exception e)
		{
			Log.e("Errrrrrr", e.getMessage());
		}
		handler.postDelayed(t, 5000);
	}

	private static Runnable t = new Runnable()
	{
		@Override
		public void run()
		{
			if (mediaPlayer != null)
			{
				mediaPlayer.reset();
			}
		}
	};
}
